package com.sourcetrip.arraylist;


import com.sourcetrip.raw.model.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author: ZhouBert
 * @date: 2020/11/16
 * @description:	测试 对象的 clone
 * 发现 clone 是 protect 的方法，于是尝试用反射调用一下
 * 结果发现居然报错：Exception in thread "main" java.lang.NoSuchMethodException: model.Student.clone()
 * 哎，不知道怎么造成 CloneNotSupportedException 异常了
 */
public class CloneTest01 {

	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
		Student jack = new Student("jack", 10);
		String packageName = "model.Student";
		Class<?> clazz = Class.forName(packageName);

		Constructor<?> constructor = clazz.getDeclaredConstructor(String.class, Integer.class);
		String name="jack";
		Integer age=11;
		//反射创建对象
		Student student = (Student) constructor.newInstance(name, age);
		System.out.println("student = " + student);

		// 居然木有这个 clone 方法？？
		Method clone = clazz.getDeclaredMethod("clone");
		clone.setAccessible(true);

		clone.invoke(student);
	}


}
